package com.ozner.cup.UIView;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ozner.cup.R;

/**
 * Created by ozner_67 on 2017/7/14.
 * 邮箱：xinde.zhang@cftcn.com
 */

public class OznerSwitchButton extends RelativeLayout implements View.OnClickListener {
    private static final String TAG = "OznerSwitchButton";
    private ImageView ivThumb = null;
    private Drawable drawablethumb = null;
    private int bgColor = 0x00fafafa;
    private Paint linePaint = null;
    private int lineColor = 0xff168ffe;
    private float lineWidth = 3f;//dp
    private boolean state = false;//标识当前switchbutton所处的状态，false未激活，true激活
    private ObjectAnimator slideLeftAnimator = null;//按钮向左滑动的动画
    private ObjectAnimator slideRightAnimator = null;//按钮向右滑动的动画
    private TransitionDrawable transitionDrawable = null;//按钮切换图片资源的动画
    private int width, height;
    private onCheckedChangedListener checkListener = null;

    public interface onCheckedChangedListener {
        void onCheckedChanged(RelativeLayout view, boolean isChecked);
    }

    public void setOnCheckedChangedListener(onCheckedChangedListener listener) {
        this.checkListener = listener;
    }

    public void checked(boolean isChecked) {
        if (isChecked) {
            LayoutParams params = new LayoutParams(height, height);
            params.addRule(ALIGN_PARENT_RIGHT, TRUE);
            ivThumb.setLayoutParams(params);
            transitionDrawable.startTransition(300);
        } else {
            LayoutParams params = new LayoutParams(height,height);
            params.addRule(ALIGN_PARENT_LEFT, TRUE);
            ivThumb.setLayoutParams(params);
            transitionDrawable.reverseTransition(300);
        }
        state = isChecked;
    }

    public OznerSwitchButton(Context context) {
        super(context);
        initTools(context);
    }

    public OznerSwitchButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
        initTools(context);
    }

    public OznerSwitchButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
        initTools(context);
    }


    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OznerSwitchButton);
        drawablethumb = array.getDrawable(R.styleable.OznerSwitchButton_thumb);
        lineColor = array.getColor(R.styleable.OznerSwitchButton_lineColor, 0xff168ffe);
        lineWidth = array.getDimension(R.styleable.OznerSwitchButton_lineWidth, 3f);
        array.recycle();
    }

    private void initTools(Context context) {
        ivThumb = new ImageView(context);
        if (drawablethumb == null) {
            drawablethumb = ContextCompat.getDrawable(context, R.drawable.kettle_switcher_thum_selector);
        }
        ivThumb.setImageDrawable(drawablethumb);
        this.addView(ivThumb);
        this.setBackgroundColor(bgColor);
        linePaint = new Paint();
        linePaint.setColor(lineColor);
        linePaint.setStyle(Paint.Style.FILL);
        linePaint.setStrokeCap(Paint.Cap.ROUND);
        linePaint.setStrokeWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, lineWidth, getResources().getDisplayMetrics()));
        //获取动画切换动画
        transitionDrawable = (TransitionDrawable) ivThumb.getDrawable();
        this.setOnClickListener(this);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        width = w;
        height = h;
//        initAnimator();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        initAnimator();

        canvas.drawColor(bgColor);
        int startX = height / 4;
        int startY = height / 2;
        int endX = width - height / 4;
        int endY = height / 2;
        canvas.drawLine(startX, startY, endX, endY, linePaint);

        super.onDraw(canvas);
    }

    private void initAnimator(){
        if (slideRightAnimator == null) {
            //设置按钮图片对应的参数
            LayoutParams params = new LayoutParams(getMeasuredHeight(), getMeasuredHeight());
            params.addRule(ALIGN_PARENT_LEFT, TRUE);
            ivThumb.setLayoutParams(params);

            //设置右滑动动画
            slideRightAnimator = ObjectAnimator.ofFloat(ivThumb, "translationX", 0, getMeasuredWidth() - getMeasuredHeight());
            slideRightAnimator.setInterpolator(new DecelerateInterpolator()); //此处为动画插值器，用于设置动画曲线，不懂的同学百度一下~~~
            slideRightAnimator.setDuration(300);
        }
        if (slideLeftAnimator == null) {
            //设置做滑动动画
            slideLeftAnimator = ObjectAnimator.ofFloat(ivThumb, "translationX", getMeasuredWidth() - getMeasuredHeight(), 0);
            slideLeftAnimator.setInterpolator(new DecelerateInterpolator());
            slideLeftAnimator.setDuration(300);
        }
    }

    @Override
    public void onClick(View v) {
        if (!state) {
            slideRightAnimator.start();
            transitionDrawable.startTransition(300);
        } else {
            slideLeftAnimator.start();
            transitionDrawable.reverseTransition(300);
        }
        state = !state;
        Log.e(TAG, "onClick: " + state);
        if(checkListener!=null){
            checkListener.onCheckedChanged(this,state);
        }
    }
}
